package com.example.demo.controller;

import com.example.demo.entity.Coupon;
import com.example.demo.service.CouponService;
import com.example.demo.controller.vo.ResponseResult;
import com.example.demo.utils.CustomDateFormatter;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/admin/coupon")
public class CouponController {

    @Autowired
    private CouponService couponService;

    @PostMapping
    public ResponseResult<Coupon> createCoupon(@RequestHeader("token") String token,
            @RequestParam String name,
            @RequestParam Integer type,
            @RequestParam Integer value,
            @RequestParam Integer amount,
            @RequestParam Integer min_price,
            @RequestParam String start_time,
            @RequestParam String end_time,
            @RequestParam Integer order) {
        
        log.info("Creating new coupon: name={}, type={}, value={}", name, type, value);
        
        Coupon coupon = new Coupon();
        coupon.setName(name);
        coupon.setType(type);
        coupon.setValue(value);
        coupon.setAmount(amount);
        coupon.setMin_price(min_price);
        coupon.setStart_time(CustomDateFormatter.isoToDate(start_time));
        coupon.setEnd_time(CustomDateFormatter.isoToDate(end_time));
        coupon.setOrder(order);
        
        Coupon createdCoupon = couponService.createCoupon(coupon);
        log.info("Coupon created successfully: id={}", createdCoupon.getId());
        return ResponseResult.success(createdCoupon);
    }

    @PostMapping("/{id}")
    public ResponseResult<Boolean> updateCoupon(
            @RequestHeader("token") String token,
            @PathVariable Integer id,
            @RequestParam String name,
            @RequestParam Integer type,
            @RequestParam Integer value,
            @RequestParam Integer amount,
            @RequestParam Integer min_price,
            @RequestParam String start_time,
            @RequestParam String end_time,
            @RequestParam Integer order,
            @RequestParam Integer status) {
        
        log.info("Updating coupon: id={}, name={}", id, name);
        
        Coupon coupon = new Coupon();
        coupon.setId(id);
        coupon.setName(name);
        coupon.setType(type);
        coupon.setValue(value);
        coupon.setAmount(amount);
        coupon.setMin_price(min_price);
        coupon.setStart_time(CustomDateFormatter.isoToDate(start_time));
        coupon.setEnd_time(CustomDateFormatter.isoToDate(end_time));
        coupon.setOrder(order);
        coupon.setStatus(status);
        
        couponService.updateCoupon(coupon);
        log.info("Coupon updated successfully: id={}", id);
        return ResponseResult.success(true);
}

    @PostMapping("/{id}/update_status")
    public ResponseResult<Boolean> updateCouponStatus(
            @RequestHeader("token") String token,
            @PathVariable Integer id,
            @RequestParam Integer status) {
        log.info("Updating coupon status: id={}, status={}", id, status);
        couponService.updateCouponStatus(id, status);
        log.info("Coupon status updated successfully: id={}", id);
        return ResponseResult.success(true);
    }

    @GetMapping("/{page}")
    public ResponseResult<?> getCouponList(
            @RequestHeader("token") String token,
            @PathVariable Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(required = false) String keyword) {
        log.info("Fetching coupon list: page={}, limit={}, keyword={}", page, limit, keyword);
        Map<String, Object> result = couponService.getCouponList(page, limit, keyword);
        return ResponseResult.success(result);
    }

    @PostMapping("/{id}/delete")
    public ResponseResult<Boolean> deleteCoupon(
            @RequestHeader("token") String token,
            @PathVariable Integer id) {
        log.info("Deleting coupon: id={}", id);
        boolean result = couponService.deleteCoupon(id);
        log.info("Coupon deleted successfully: id={}", id);
        return ResponseResult.success(result);
    }
}
